package com.example.provider.javastudy.thread.common;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.example.demo.thread
 * @author: 红雪（hongxue@maihaoche.com）
 * @date: 2019/1/16 9:20 AM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 * 注意：本内容仅限于卖好车内部传阅，禁止外泄以及用于其他的商业项目
 */
public class TestThread {
    public static void main(String[] args) {
        Hero gareen = new Hero();
        gareen.name = "盖伦";
        gareen.hp = 616;
        gareen.damage = 50;

        Hero teemo = new Hero();
        teemo.name = "提莫";
        teemo.hp = 300;
        teemo.damage = 30;

        Hero bh = new Hero();
        bh.name = "赏金猎人";
        bh.hp = 500;
        bh.damage = 65;

        Hero leesin = new Hero();
        leesin.name = "盲僧";
        leesin.hp = 455;
        leesin.damage = 80;

     /*  KillThread killThread = new KillThread(gareen,teemo);
       KillThread killThread1 = new KillThread(bh,leesin);
        killThread.start();
        killThread1.start();*/

        //匿名类
        Thread thread1 = new Thread(new Runnable() {

        @Override
        public void run(){
            while (!teemo.isDead()){
                gareen.attackHero(teemo);
            }
        }
        },"线程一");
        //thread.setPriority(Thread.MIN_PRIORITY);
        thread1.start();




        Thread thread2 = new Thread(new Runnable() {

            @Override
            public void run(){
                while (!leesin.isDead()){
                    //临时暂停，使得t1可以占用cpu资源
                    Thread.yield();
                    bh.attackHero(leesin);
                }
            }
        },"线程二");
        //thread1.setPriority(Thread.MAX_PRIORITY);
        thread2.start();

       /* try {
            //加到主线程中,线程的准备也是要代码一步步运行的，运行到第一个线程时，该线程就绪，等待调度。运行到第二个线程时，就绪，等待调度。然后再执行，join方法（父线程等待子线程运行完毕才执行下面的代码），会导致俩线程都在运行。
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程等待子线程thread运行已完毕");*/

        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("这是守护线程，记录日志的时间为："+new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()));
                }
            }
        },"守护线程");
        thread3.setDaemon(true);
        thread3.start();

    }
}
